const app = getApp()
import {
    showMsg
} from '../../../../utils/config.js'
import {
    reqDoorList,
    reqInfraredList,
} from '../../../../request/api'
Page({
    data: {
        openDoor: false, //开门告警
        closeDoor: false, //关门告警
        height: '480rpx',
        navHeight: app.globalData.navHeight + 'px', //头部距离
        bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离
        currentTab:0,
        deviceInfo: {},
        dataList: [],
        page: 1,
        isNextPage: true, //是否下一页
        triggered: false, //是否下拉刷新列表
        srcFraredActive: '/statics/images/device/infrared_active.png',
        srcInfraredActive: '/statics/images/device/infrared_inactive.png',
        empty: '/statics/images/empty.png',
        deviceInfo:{},
        flag:false,
    },
    onShow() {
       
    },
    onLoad(e) {
        console.log(JSON.parse(e.deviceInfo));
        let deviceInfo = JSON.parse(e.deviceInfo)
        this.setData({
            deviceInfo
        })
        if(deviceInfo.flag){
            this.setData({
                flag:true
            })
        }
        this.getDataList()
    },
   
    async getDataList() {
        let params = {
            imei: this.data.deviceInfo.imei,
            spaceId: this.data.deviceInfo.spaceId||''
        }
        let {
            deviceType
        } = this.data.deviceInfo
        if (deviceType === "DoorMagnetism") {
            // 门磁
            const res = await reqDoorList(this.data.page, 18, params)
            if (res.code !== 20000) return showMsg(res.message)
            let {
                rows
            } = res.data
            if (this.data.page === 1) {
                this.setData({
                    dataList: rows,
                    isNextPage: true
                })
            } else if (this.data.page > 1 && rows.length) {
                this.setData({
                    dataList: this.data.dataList.concat(rows),
                    isNextPage: true
                })
            } else if (this.data.page > 1 && !rows.length) {
                showMsg('已经到底啦~')
                this.setData({
                    isNextPage: false
                })
            }
        } else if (deviceType === "Infrared") {
            // 红外
            const res = await reqInfraredList(this.data.page, 18, params)
            if (res.code !== 20000) return showMsg(res.message)
            let {
                rows
            } = res.data
            if (this.data.page === 1) {
                this.setData({
                    dataList: rows,
                    isNextPage: true
                })
            } else if (this.data.page > 1 && rows.length) {
                this.setData({
                    dataList: this.data.dataList.concat(rows),
                    isNextPage: true
                })
            } else if (this.data.page > 1 && !rows.length) {
                showMsg('已经到底啦~')
                this.setData({
                    isNextPage: false
                })
            }
        } else {
            return
        }
    },
    // 滚动到底部触发
    lower() {
        if (!this.data.isNextPage) return
        this.setData({
            page: this.data.page + 1,
        })
        this.getDataList()
    },
    // 下拉刷新
    async onRefresh() {
        this.setData({
            dataList: [],
            page: 1,
            isNextPage: true,
            triggered: true,
        })
        await this.getDataList()
        this.setData({
            triggered: false,
        })
    },
})